package com.dc.config.http;

import org.springframework.boot.web.client.RestTemplateCustomizer;
import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

public class MyRestTemplateCustomizer implements RestTemplateCustomizer {

    @Resource
    private MyRequestInterceptor myRequestInterceptor;

    @Override
    public void customize(RestTemplate restTemplate) {
        restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(restTemplate.getRequestFactory()));
        restTemplate.getInterceptors().add(myRequestInterceptor);
    }

//    @Override
//    public void customize(RestTemplate restTemplate) {
//        HttpRoutePlanner routePlanner = new CustomRoutePlanner(new HttpHost("proxy.example.com"));
//        HttpClient httpClient = HttpClientBuilder.create().setRoutePlanner(routePlanner).build();
//        restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient));
//    }

}

//class CustomRoutePlanner extends DefaultProxyRoutePlanner {
//
//    CustomRoutePlanner(HttpHost proxy) {
//        super(proxy);
//    }
//
//    @Override
//    public HttpHost determineProxy(HttpHost target, HttpRequest request, HttpContext context) throws HttpException {
//        if (target.getHostName().equals("192.168.0.5")) {
//            return null;
//        }
//        return super.determineProxy(target, request, context);
//    }
//
//}